package edu.tgc.entity.system;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(indexes = { @Index(columnList = "enabled asc"), @Index(columnList = "sort asc") })
public class SysRole extends BaseAuditableDateEntity {

    private static final long serialVersionUID = 3734772313185439822L;

    @JsonIgnore
    private Set<SysDept> depts = new HashSet<>();
    protected String description;
    protected Boolean enabled = true;
    protected String name;

    @JsonIgnore
    private Set<SysPermission> permissions = new HashSet<>();
    protected Integer sort = 1;
    @JsonIgnore
    private Set<SysUser> users = new HashSet<>();

    public SysRole() {
    }

    public SysRole(UUID id) {
        this.id = id;
    }

    @ManyToMany
    @JoinTable(name = "sys_dept_role", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "dept_id"))
    public Set<SysDept> getDepts() {
        return this.depts;
    }

    @Column(name = "description", length = 255)
    public String getDescription() {
        return this.description;
    }

    @Column(name = "enabled")
    public Boolean getEnabled() {
        return this.enabled;
    }

    @Column(name = "name", length = 31)
    public String getName() {
        return this.name;
    }

    @OrderBy(value = "sort asc")
    @ManyToMany
    @JoinTable(name = "sys_role_permission", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "permission_id"))
    public Set<SysPermission> getPermissions() {
        return this.permissions;
    }

    @Column(name = "sort")
    public Integer getSort() {
        return this.sort;
    }

    @ManyToMany
    @JoinTable(name = "sys_user_role", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
    public Set<SysUser> getUsers() {
        return this.users;
    }

    public void setDepts(Set<SysDept> depts) {
        this.depts = depts;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPermissions(Set<SysPermission> permissions) {
        this.permissions = permissions;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public void setUsers(Set<SysUser> users) {
        this.users = users;
    }

}
